********************************************************************************************************************************
***   Replication file for:                                                                                                  ***
***   Rafiee Rad, S., Braun, S. T., and Roy, O. (2023). Anchoring as a Structural Bias of Deliberation.					     ***
***   							                                                                                             ***
***   SCRIPT: 		1_analysis_agent_level.do																				 ***	
***   PURPOSE: 		script for analysis of agent level data																	 ***
***   REPLICATES:	Figure:	1, 2, A-1																								
***					Table:  1, 2, A-1, A-2, A-3, A-4, A-5, A-6 
********************************************************************************************************************************

* Preamble (unnecessary when executing run.do)
run "$anchoring/scripts/programs/_config.do"

***************
* Code begins
***************

******************
*** Read in agent-level data

use "$anchoring/processed/data_agent_level.dta", clear


** Replace distance measures with missing if not transitive
foreach x in dp ks cs{
    replace distancetoagent`x' = . if d_`x'transitive == 0
}

** Normalize distance measures by maximum theoretical distance (DP: 4; KS: 6; CS: 4)

gen rel_distancetoagentdp = distancetoagentdp / 4
gen rel_distancetoagentcs = distancetoagentcs / 4
gen rel_distancetoagentks = distancetoagentks / 6


** Generate explanatory variables 

* Generate variable for alignment with first speakers prefernces
gen share_agents_first_pref = (freqvoteag1-1) / (groupsize-1)
label var share_agents_first_pref "Share of agents (other than the first speaker) that share first speaker's preference"

* Herfindahl index for similarity of preferences
gen preference_similarity = (numvotes0 / groupsize)^2 
forvalues i = 1(1)12{
	replace preference_similarity = preference_similarity + (numvotes`i' / groupsize)^2
}
label var preference_similarity "Herfindahl index of preference similarity"

egen zpreference_similarity = std(preference_similarity)
label var zpreference_similarity "Herfindahl index of preference similarity, standardized"


* Interaction terms, agent level
gen inter_first_expert = d_firstspeaker * d_expert

* Interaction terms, group level
gen inter_first_size = d_firstspeaker * groupsize
gen inter_first_pref1 = d_firstspeaker * share_agents_first_pref
gen inter_first_pref2 = d_firstspeaker * zpreference_similarity
gen inter_expert_group = d_expert * diff_expertise_expert_and_group
gen inter_first_reliability = d_firstspeaker * reliability


*** Replicate Figures 1 and A-1
*** Average distance to original ranking by agent type and group size


* Boolean measure (Minimum)

preserve

collapse (mean) d_min_distance_dp d_min_distance_ks d_min_distance_cs, by(groupsize d_expert d_firstspeaker)

twoway (connected d_min_distance_dp groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected d_min_distance_dp groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected d_min_distance_dp groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected d_min_distance_dp groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)1.0, nogrid) xlabel(0(10)100, nogrid) ytitle("Minimum distance (0/1)", margin(normal)) 
graph export "$anchoring/results/figures/Fig1a.eps", replace

twoway (connected d_min_distance_ks groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected d_min_distance_ks groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected d_min_distance_ks groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected d_min_distance_ks groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)1.0, nogrid) xlabel(0(10)100, nogrid) ytitle("Minimum distance (0/1)", margin(normal)) 
graph export "$anchoring/results/figures/FigA1a.eps", replace

twoway (connected d_min_distance_cs groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected d_min_distance_cs groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected d_min_distance_cs groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected d_min_distance_cs groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)1.0, nogrid) xlabel(0(10)100, nogrid) ytitle("Minimum distance (0/1)", margin(normal)) 
graph export "$anchoring/results/figures/FigA1c.eps", replace

restore

* Continuous measure (Distance)

preserve

collapse (mean) rel_distancetoagentdp rel_distancetoagentks rel_distancetoagentcs, by(groupsize d_expert d_firstspeaker)

twoway (connected rel_distancetoagentdp groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected rel_distancetoagentdp groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected rel_distancetoagentdp groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected rel_distancetoagentdp groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)0.5, nogrid) xlabel(0(10)100, nogrid) ytitle("Normalized distance", margin(normal)) 
graph export "$anchoring/results/figures/Fig1b.eps", replace

twoway (connected rel_distancetoagentks groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected rel_distancetoagentks groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected rel_distancetoagentks groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected rel_distancetoagentks groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)0.5, nogrid) xlabel(0(10)100, nogrid) ytitle("Normalized distance", margin(normal)) 
graph export "$anchoring/results/figures/FigA1b.eps", replace

twoway (connected rel_distancetoagentcs groupsize if d_expert == 0 & d_firstspeaker == 1, sort mcolor(gs0) msymbol(circle) msize(medsmall) lcolor(gs0) lpattern(solid)) (connected rel_distancetoagentcs groupsize if d_expert == 1 & d_firstspeaker == 0, sort mcolor(gs0) msymbol(diamond) msize(medsmall) lcolor(gs0) lpattern(dash)) (connected rel_distancetoagentcs groupsize if d_expert == 1 & d_firstspeaker == 1, sort  mcolor(gs8) msymbol(triangle) msize(medsmall) lcolor(gs8) lpattern(solid)) (connected rel_distancetoagentcs groupsize if d_expert == 0 & d_firstspeaker == 0, sort  mcolor(gs8) msymbol(square) msize(medsmall) lcolor(gs8) lpattern(dash_dot)), graphregion(fcol(white) lcol(white)) legend(pos(6) row(1) lab(1 "First") lab(2 "Highest weight") lab(3 "First & weight") lab(4 "Other")) xtitle("Group size", margin(normal)) ylabel(0.0(0.1)0.6, nogrid) xlabel(0(10)100, nogrid) ytitle("Normalized distance", margin(normal)) 
graph export "$anchoring/results/figures/FigA1d.eps", replace


restore


*** Replicate Figure 2 (Effect of speaking position on distance relative to last speaker)
tab agent, gen(d_agent)

reg rel_distancetoagentdp d_agent1-d_agent50 d_expert if groupsize == 51, cluster(ID)

coefplot, keep(d_agent*) ytitle("Effect of speaking position (relative to last)") xtitle("Speaking position") yline(0, lcolor(red)) ylabel(-0.25(0.05)0, nogrid) graphregion(color(white)) nokey vertical pstyle(p1) nooffsets coeflabels(d_agent1 = "1" d_agent2 = " " d_agent3 = " " d_agent4 = " " d_agent5 = "5" d_agent6 = " " d_agent7 = " " d_agent8 = " " d_agent9 = " " d_agent10 = "10" d_agent11 = " " d_agent12 = " " d_agent13 = " " d_agent14 = " " d_agent15 = "15" d_agent16 = " " d_agent17 = " " d_agent18 = " " d_agent19 = " " d_agent20 = "20" d_agent21 = " " d_agent22 = " " d_agent23 = " " d_agent24 = " " d_agent25 = "25" d_agent26 = " " d_agent27 = " " d_agent28 = " " d_agent29 = " " d_agent30 = "30" d_agent31 = " " d_agent32 = " " d_agent33 = " " d_agent34 = " " d_agent35 = "35" d_agent36 = " " d_agent37 = " " d_agent38 = " " d_agent39 = " " d_agent40 = "40" d_agent41 = " " d_agent42 = " " d_agent43 = " " d_agent44 = " " d_agent45 = "45" d_agent46 = " " d_agent47 = " " d_agent48 = " " d_agent49 = " " d_agent50 = "50") ciopts(lcolor(black)) mcolor(black)

graph export "$anchoring/results/figures/Fig2.eps", replace 


*** Replicate Table 1, A-1, A-2 (Baseline results for the impact of first speaker, highest weight)

foreach x in dp cs ks{

areg rel_distancetoagent`x' d_firstspeaker d_expert, cluster(ID) absorb(groupsize)
est store col1
areg rel_distancetoagent`x' d_firstspeaker reliability, cluster(ID) absorb(groupsize)
est store col2
areg rel_distancetoagent`x' d_firstspeaker d_expert share_agents_first_pref, cluster(ID) absorb(groupsize)
est store col3
areg rel_distancetoagent`x' d_firstspeaker d_expert zpreference_similarity, cluster(ID) absorb(groupsize)
est store col4

outreg2 [col1 col2 col3 col4] using "$anchoring/results/tables/table_baseline_`x'.tex",  dec(3) replace

}


*** Replicate Tables 2, A-3, A-4 (Mediators: Interactions between anchoring and other characteristics)

foreach x in dp cs ks{

areg rel_distancetoagent`x' d_firstspeaker inter_first_expert d_expert, cluster(ID) absorb(groupsize)
est store col1
areg rel_distancetoagent`x' d_firstspeaker inter_first_reliability reliability, cluster(ID) absorb(groupsize)
est store col2
areg rel_distancetoagent`x' d_firstspeaker inter_first_pref1 d_expert share_agents_first_pref, cluster(ID) absorb(groupsize)
est store col3
areg rel_distancetoagent`x' d_firstspeaker inter_first_pref2 d_expert zpreference_similarity, cluster(ID) absorb(groupsize)
est store col4

outreg2 [col1 col2 col3 col4] using "$anchoring/results/tables/table_mediators_`x'.tex",  dec(3) replace

}

*** Replicate Table A-5 (Baseline result for Boolean distance measure (0/1))

areg d_min_distance_dp d_firstspeaker d_expert, cluster(ID) absorb(groupsize)
est store col1
areg d_min_distance_dp d_firstspeaker reliability, cluster(ID) absorb(groupsize)
est store col2
areg d_min_distance_dp d_firstspeaker d_expert share_agents_first_pref, cluster(ID) absorb(groupsize)
est store col3
areg d_min_distance_dp d_firstspeaker d_expert zpreference_similarity, cluster(ID) absorb(groupsize)
est store col4

outreg2 [col1 col2 col3 col4] using "$anchoring/results/tables/table_baseline_boolean.tex",  dec(3) replace


*** Replicate Table A-6 (Mediators: Interactions between anchoring and other characteristics for Boolean distance measure (0/1))


areg d_min_distance_dp d_firstspeaker inter_first_expert d_expert, cluster(ID) absorb(groupsize)
est store col1
areg d_min_distance_dp d_firstspeaker inter_first_reliability reliability, cluster(ID) absorb(groupsize)
est store col2
areg d_min_distance_dp d_firstspeaker inter_first_pref1 d_expert share_agents_first_pref, cluster(ID) absorb(groupsize)
est store col3
areg d_min_distance_dp d_firstspeaker inter_first_pref2 d_expert zpreference_similarity, cluster(ID) absorb(groupsize)
est store col4

outreg2 [col1 col2 col3 col4] using "$anchoring/results/tables/table_mediators_boolean.tex",  dec(3) replace


*** Replicate Table A-7 (The effect of anchoring and weight with immodest agents)

use "$anchoring/processed/data_agent_level_immodest.dta", clear

** Replace distance measures with missing if not transitive
foreach x in dp ks cs{
    replace distancetoagent`x' = . if d_`x'transitive == 0
}

** Normalize distance measures by maximum theoretical distance (DP: 4; KS: 6; CS: 4)

gen rel_distancetoagentdp = distancetoagentdp / 4
gen rel_distancetoagentcs = distancetoagentcs / 4
gen rel_distancetoagentks = distancetoagentks / 6

** Interaction between first speaker and expert
gen inter_first_expert = d_firstspeaker * d_expert


areg rel_distancetoagentdp d_firstspeaker inter_first_expert d_expert, cluster(ID) absorb(groupsize)
est store col1
areg rel_distancetoagentks d_firstspeaker inter_first_expert d_expert, cluster(ID) absorb(groupsize)
est store col2
areg rel_distancetoagentcs d_firstspeaker inter_first_expert d_expert, cluster(ID) absorb(groupsize)
est store col3
outreg2 [col1 col2 col3] using "$anchoring/results/tables/table_immodest.tex",  dec(3) replace




*** EOF